
import { defineStore } from 'pinia'
import {
    getAccessToken,
    setAccessToken,
    removeAccessToken,
    getUser,
    setUser,
    removeUser,
    getActId,
    removeActId
} from "@/utils/auth";
// import { getUserInfo, login } from '@/api/user'
// import type { UserState } from '@/types/user';
export const useUserStore = defineStore("user", {
    state: (): any => ({
        token: getAccessToken(),
        user: getUser()
    }),

    actions: {

        /**
         * @description 设置token
         * @param {*} token
         */
        setToken(token: string) {
            this.token = token
            setAccessToken(token)
        },

        /**
         * @description 登录
         * @param {*} form
         */
        async login(form: any) {
            // const { token }: any = await login(form)
            const token = form.toString()
            if (token) {
                this.setToken(token)
                this.getUserInfo()
            } else {
                const res = `登录接口异常`
                throw res
            }
        },

        /**
         * @description 获取用户信息接口
         * @returns
         */
        async getUserInfo() {
            // const {} = await getUserInfo()
            const userInfo = {
                id: 1,
                name: "闫小猪🐷",
                sex: '1',
            }
            this.user = userInfo
            setUser(userInfo)
        },

        /**
         * @description 退出登录
         */
        async logout() {
            await removeUser()
            await removeAccessToken()
            // await location.reload()
        }

    }
})

